<?php
/*
 * Creative Bulletin Board Generation 3
 * Version 1.0 Beta 1
 +------------------------------------
 * User Application, Registration Module (Frontend)
 * Started: 27 September 2009 9:12 AM
 * Script version: 1.0
 +------------------------------------
 */

class module implements module_interface{
	public function returninitvars(){
		global $lang;
		$this->info["page_title"] = $lang->getByKey("user.register");
		$this->info["page_description"] = "";
		$this->info["module_version"] = "1.0";
		return $this->info;
	}

	public function construct(){
		global $db, $view, $lang;
		$this->run = ($db->conf_value("can_register") == "1" ? true : false);
		if(!$this->run){
			$view->writeIn($view->inlineError($lang->getByKey("user.register.disabled")));
			$this->custom_error = true; # Turn off module error handler
		}
	}
	
	public function run(){
		global $_CF; // Using the new $_CF instead of the older classes.
		if(isset($_SESSION["cf_username"], $_SESSION["cf_controller"])){
			$_CF->view->writeIn($_CF->view->inlineWarning($_CF->lang->getByKey("user.register.notguest")));
		}
		else{
			// So we first... Uhh... Steps.
			switch((isset($_GET["step"]) ? intval($_GET["step"]) : 1)){
				case 1:
					$data['rules'] = $_CF->db->conf_value("register_rules");
					$this->output = $_CF->view->showTemplate("register_step1", $data); # Terms and Conditions / Rules
				break;
				case 2:
					$data['errors'] = "";
					$data['login'] = "";
					$data['display'] = "";
					$data['pass'] = "";
					$data['pass2'] = "";
					$data['email'] = "";
					$data['email2'] = "";
					$this->output = $_CF->view->showTemplate("register_step2", $data); # Form
				break;
				case 3:
					// Validate things!
					if(!isset(
						$_POST['loginname'],
						$_POST['displayname'],
						$_POST['password'],
						$_POST['password2'],
						$_POST['email'],
						$_POST['email2']
					)){
						//'s go back to part 1.
						$data['rules'] = $_CF->db->conf_value("register_rules");
						$this->output = $_CF->view->showTemplate("register_step1", $data); # Terms and Conditions / Rules
					}
					else{
						$this->output = "";
						$login = $_CF->main->secure_string($_POST['loginname']);
						$displayname = $_CF->main->secure_string($_POST['displayname']);
						$password = $_POST['password'];
						$password2 = $_POST['password2'];
						$email = $_POST['email'];
						$email2 = $_POST['email2'];
						$data['errors'] = "";
						if($password != $password2){
							$data['errors'] .= $_CF->view->inlineError($_CF->lang->getByKey("user.register.passmatch"));
							$errors = 1;
						}
						if($email != $email2){
							$data['errors'] .= $_CF->view->inlineError($_CF->lang->getByKey("user.register.emailmatch"));
							$errors = 1;
						}
						// Validation now
						if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
							$data['errors'] .= $_CF->view->inlineError($_CF->lang->getByKey("user.register.emailinvalid"));
							$errors = 1;
						}
						if(isset($errors)){
							// HALT! Lets show the ugly form again.
							$data['login'] = $login;
							$data['display'] = $displayname;
							$data['pass'] = $password;
							$data['pass2'] = $password2;
							$data['email'] = $email;
							$data['email2'] = $email2;
							$this->output .= $_CF->view->showTemplate("register_step2", $data);
						}
						else{
							// Create the account.
							if($_CF->models->users->create($login, $password, $email, $displayname)){
								$this->output .= $_CF->view->inlineSuccess($_CF->lang->getByKey("user.register.success"), "index.php?app=user&module=login");
							}
							else{
								$this->output .= $_CF->view->inlineError($_CF->lang->getByKey("user.register.internal"));
							}
						}
					}
				break;
			}
			$_CF->view->writeIn(
				$_CF->view->tablePadded($_CF->lang->getByKey("user.register.form"),
								   $this->output,
								   "add"
								  )
						  );
		}
	}

	public function destruct(){}
}
